package com.jade.store.crm.service.impl;

import com.jade.bss.DbException;
import com.jade.bss.base.RoleAction;
import com.jade.bss.base.customer.Customer;
import com.jade.bss.base.utils.CopyBean;
import com.jade.bss.base.utils.MsgUtil;
import com.jade.bss.db.impl.BaseFacadeImpl;
import com.jade.framework.base.context.ApplicationContextUtils;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.Restrictions;
import com.jade.framework.base.util.PagingList;
import com.jade.framework.base.util.PagingResult;
import com.jade.zsy.base.common.StatusConstant;
import com.jade.zsy.base.facade.BusinessCodeFacade;
import com.jade.zsy.crm.dao.RechargeDao;
import com.jade.zsy.crm.entity.Recharge;
import com.jade.zsy.crm.service.RechargeFacade;
import com.jade.zsy.crm.vo.RechargeVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * 会员充值
 * User: JinYa 2009_jinya@163.com
 * Date: 2016-5-5
 * Time: 10:53
 * Version : 1.0
 * Remark:
 * History:
 */
@Service("RechargeFacade")
public class RechargeFacadeImpl extends BaseFacadeImpl implements RechargeFacade {
    private BusinessCodeFacade businessCodeFacade = ApplicationContextUtils.getBean("BusinessCodeFacade");
    @Inject
    @Named("RechargeDao")
    private RechargeDao entityDao;
    @Override
    @Transactional
    public void save(Object obj) throws DbException {
        Recharge entity=(Recharge)obj;
        Customer customer= (Customer) SecurityUtils.getSubject().getPrincipal();
        entity.setOwnerId(customer.getOwnerId());
        entity.setCreator(customer.getNickName());
        entity.setCreatorId(customer.getId());
        super.save(entity);
    }
    @Override
    @Transactional
    public void update(Object obj) throws DbException {
        Recharge entity=(Recharge)obj;
        Customer customer= (Customer) SecurityUtils.getSubject().getPrincipal();
        entity.setModifier(customer.getTrueName());
        super.update(entity);
    }

    @Transactional
    public void delete(long... obj) throws DbException {
        for(long id : obj){
            Recharge org=entityDao.getById(Recharge.class,id);
            if(org==null || org.getId()<=0)
                throw new DbException(MsgUtil.RECODRD_NO, MsgUtil.RECODRD_NO_TEXT);
            entityDao.delete(Recharge.class,org);
        }
    }

    @Transactional
    public void disabled(long... obj) throws DbException {
        for(long id : obj){
            Recharge org=entityDao.getById(Recharge.class,id);
            if(org==null || org.getId()<=0)
                throw new DbException(MsgUtil.RECODRD_NO, MsgUtil.RECODRD_NO_TEXT);
            org.setStatus(StatusConstant.RECORD_STOP);
            entityDao.update(org);
        }
    }

    @Transactional
    public void enabled(long... obj) throws DbException {
        for(long id : obj){
            Recharge enRecharge=entityDao.getById(Recharge.class,id);
            if(enRecharge==null || enRecharge.getId()<=0)
                throw new DbException(MsgUtil.RECODRD_NO, MsgUtil.RECODRD_NO_TEXT);
            enRecharge.setStatus(StatusConstant.RECORD_NORMAL);
            entityDao.update(enRecharge);
        }
    }

    @Override
    public PagingResult queryEntity(Object obj, int page, int size, String keyword, String... orders) throws DbException,IllegalAccessException, InstantiationException {
        Customer customer= (Customer) SecurityUtils.getSubject().getPrincipal();
        RechargeVo entity=(RechargeVo)obj;
        Criteria criteria = new Criteria((page - 1) * size, size);
        criteria.add(Restrictions.eq("ownerId",customer.getOwnerId()));
        if(StringUtils.isNotEmpty(keyword)){
            criteria.add(Restrictions.or(Restrictions.like("code", keyword),
                    Restrictions.like("name", keyword)));
        }
        criteria.addOrder(com.jade.framework.base.criterion.Order.desc(RoleAction.SORT_ITEM));
        PagingList pagingList = entityDao.queryEntity(Recharge.class, criteria);
        PagingResult  result = PagingResult.getResult(CopyBean.copy(pagingList, RechargeVo.class), pagingList.getTotalCount(), page, size);
        return result;
    }
}
